﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NotificationServer.BusinessLayer.UtilityClasses;
using NotificationServer.DataAccessLayer.DAO;
using System.Data;

namespace NotificationServer.BusinessLayer
{
    public class NotificationBO
    {
        private NotificationDAO _notificationDAO;
        private DeviceDAO _deviceDAO;
        private int _maximumNotifiactionSize = 4000;

        public NotificationBO()
        {
            _notificationDAO = new NotificationDAO();
            _deviceDAO = new DeviceDAO();
        }

        public bool SendNotification(Guid device_id, string notification)
        {

            if (device_id == null)
                // TODO: Error Handling
                return false;
            if (notification == null || notification.Length > _maximumNotifiactionSize)
                // TODO: Error Handling 
                return false;

            Guid notificationId = Guid.NewGuid();

            //1. Register the notification in DB
            if (!_notificationDAO.RegisterNotification(notificationId, device_id, notification))
                // TODO: Error Handling 
                return false;


            //2. Retrieve the registration_id from database
            DataTable da = _deviceDAO.GetRegistrationId(device_id);
            if (da.Rows.Count < 1)
                // TODO: Error Handling 
                return false;
            string registration_id = da.Rows[0]["registration_id"].ToString();

            
            //3. Send the notification to GCF Server
            GCM apnGCM = new GCM();
            return apnGCM.SendNotification(notificationId.ToString(), registration_id, notification);
        }
    }
}